package yb.ecp.fast.infra.web;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.BasicController;
import yb.ecp.fast.infra.service.GuidService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by john on 2017/10/26.
 */
@RestController
@RequestMapping("/guid")
public class GuidController extends BasicController {
    @Autowired
    private GuidService guidService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/next", method = RequestMethod.GET)
    public ActionResult nextId() {
        return actionResult(guidService.nextId());
    }

    @RequestMapping(value = "/number", method = RequestMethod.GET)
    public ActionResult number() {
        return actionResult(guidService.nextId());
    }

    @RequestMapping(value = "/batchNumber", method = RequestMethod.GET)
    public ActionResult<List<Long>> batchNumber(@RequestParam("amount") int amount) {
        if (amount <= 0) {
            return actionResult(new ArrayList<>(0));
        }
        List<Long> numberList = new ArrayList<>(amount);
        for (int i = 0; i < amount; i++) {
            numberList.add(guidService.nextId());
        }
        return actionResult(numberList);
    }

    @RequestMapping(value = "/text", method = RequestMethod.GET)
    public ActionResult text() {
        return actionResult(Long.toString(guidService.nextId()));
    }

    @RequestMapping(value = "/batchText", method = RequestMethod.GET)
    public ActionResult<List<String>> batchText(@RequestParam("amount") int amount) {
        if (amount <= 0) {
            return actionResult(new ArrayList<>(0));
        }
        List<String> textList = new ArrayList<>(amount);
        for (int i = 0; i < amount; i++) {
            textList.add((Long.toString(guidService.nextId())));
        }
        return actionResult(textList);
    }

    @ApiOperation("redis示例接口，获取redis中的某个key的value")
    @RequestMapping(value = "/redisDemo", method = RequestMethod.GET)
    public String redisDemo(String key) {
        return redisTemplate.opsForValue().get(key);
    }

}
